
<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> 
<title>实时获取——恒拓运价魔方</title>
<script type="text/javascript">
 var baseURL = "<%=request.getContextPath() %>";
</script>
<link href="<%=request.getContextPath() %>/fare/css/base.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/js/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/js/datePicker/WdatePicker.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/fare/lib/ligerUI/js/ligerui.min.js"></script>
<link href="<%=request.getContextPath() %>/fare/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href='<%=request.getContextPath()%>/fare/js/cityquery/cityquery.css' rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/jsFile/fdc.js"></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/fare/js/cityquery/citylist.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/fare/js/cityquery/querycity.js'></script>
<link rev="stylesheet" media="all" href="<%=request.getContextPath() %>/fare/css/form_style.css" type="text/css" rel="stylesheet">
<!-- 圆角文本框 -->
<link href="<%=request.getContextPath() %>/fare/css/htc/htc.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"
	src="<%=request.getContextPath() %>/jsFile/FDCCrawl/js/FDCCrawl.js"></script>	
<style>
	#juzheng_table tr{
		height:40px;
		
	}
	
	.juhua{
		width:24px;
		height:24px;
	}
</style>

<script type="text/javascript">
var _TIMING=10*60*1000;//时限
$(function(){
	$('#depCity').querycity({'data':citysFlight,'tabs':labelFromcity,'hotList':hotList});
	$('#arrCity').querycity({'data':citysFlight,'tabs':labelFromcity,'hotList':hotList});
	$("#flightDate").val(generateTimeByFutrueDay(0));
	initQueryBtn();
});
/**
 * 生成一个未来天数的日期字符串
 */
function generateTimeByFutrueDay(day){
	if(!/^\d+$/.test(day)){day=0;}
	var now=new Date();
	now.setDate(now.getDate()+day);
	var year=now.getFullYear();
	var month=now.getMonth()+1;
	month=month<10?"0"+month:month+"";
	var date=now.getDate();
	date=date<10?"0"+date:date+"";
	return year+"-"+month+"-"+date;
}


function initQueryBtn(){
	
	var lastCrawlTime="${session.LAST_CRAWL_TTME}";
	lastCrawlTime=lastCrawlTime?parseFloat(lastCrawlTime):-1;
	var now=new Date();
	var has=now.getTime()-lastCrawlTime;
	if(lastCrawlTime==-1 || has>=_TIMING){
		setCrawlBtnCanUse();
	}else{
		setCrawlBtnCanNotUse(has);
	}
}
//计时
function timeCount(has,str){
	var timingStr=str||"距离下次查询时间还有 ";
	has-=1000;
	if(has<=0){
		setCrawlBtnCanUse();
		return;
	}
	var mm=parseInt(has/(60*1000));
	var ss=parseInt((has%(60*1000))/1000);
	mm=mm<10?"0"+mm:mm+"";
	ss=ss<10?"0"+ss:ss+"";
	$("#timingLabel").text(timingStr+"00:"+mm+":"+ss);
	setTimeout(function(){
		timeCount(has,timingStr);
	},1000);
}
//设置查询可用
function setCrawlBtnCanUse(str){
	//$("#Submit1").css({display:""});
	//$("#Submit2").css({display:""});
	//$("#Submit1").removeAttr("disabled");
	//$("#Submit1").ligerHideTip();
	//var okStr=str||"点击【查询】即时运价";
	//$("#timingLabel").text(okStr);
	//setTimeout(function(){
		//$("#Submit1").ligerHideTip();
	//},500);
}
//设置查询不可用
function setCrawlBtnCanNotUse(has,str){

	//$("#Submit1").attr("disabled",true);
	//var timingStr=str||"距离下次查询时间还有 ";
	/* 
		$("#Submit1").ligerTip({
		content:"<label id='timingLabel' style='display:none;'>"+timingStr+"</label>",
		callback:function(){
			timeCount(_TIMING-has);	
		}
	}); 
	*/
	//$("#Submit1").css({display:"none"});
	//$("#Submit2").css({display:"none"});
}
//通知服务器即将获取数据，服务器返回true/false来确定能否真正执行数据抓取
function notifyCrawl(){
	var url="<%=request.getContextPath()%>/crawl/realtime/notifyCrawl.action";
	var flag=validateCity() && validateTime();
	if(flag){
		FDC.showLoading();
		$.ajax({
            cache: false,
            type: "POST",
            url:url,
            dataType:"json",
            async: true,
            error: function(request) {
                alert("连接失败！请联系管理员");
            },
            success: function(data) {
				if(data==true){
					setCrawlBtnCanNotUse(0);
					asyncCrawl();
					FDC.hideLoading();
				}else{
					alert("无法获取数据\n可能原因：\n冷却时间未到 或者 服务器繁忙");
				}
			},
            complete:function(){
            	FDC.hideLoading();
            }
        });
	}	
}
//异步抓取
function asyncCrawl(){
	var data=$('#form1').serialize();
	var count=0;//计数
	var array=new Array();
	var url="<%=request.getContextPath()%>/crawl/realtime/crawlLowestPlaneInfoByCompany.action";
	//对每个航空公司进行数据抓取
	$(".line_price").each(function(){
		var id=this.id;
		//if(id!='MU'){return ;}
		showWait(id);
		if(id!="ALL" ){
			var newData=data+"&carrierKey="+id;//json数据拼装,加上航空公司的carrierKey
			FDCCrawl.crawlLowestPlaneInfoByCompany(newData,null,
			function(d,p){
				switch(d.status){
					case 'error':
						show(id,"获取失败","网络繁忙","red");
						break;
					case 'success':
						if(d.data==null || d.data==undefined){
							show(id,"没有航班信息","该航空公司没有此航班信息","black");
						}else{
							show(id,"￥ "+d.data.lowerPrice,detailPlaneInfoTemplate(d.data),"black");
							array.push(d.data);
						}
						break;
					//default:
						//show(id,"获取失败","网络繁忙","red");
						//break;
				};
				count++;
				if(count==8){
					alert("count:"+count);
					displaySort(array);
				};
			},
			'<%=request.getContextPath() %>',null);
		}
	});
}
//对已经load好的数据进行排序低价高亮显示等等
function displaySort(array){
	array=array?array:[];
	if(array.length>0){
		array.sort(function(a,b){
			return (a.lowerPrice-b.lowerPrice);
		});
		//排序好之后第一个是最低的，显示在ALL
		//某种情况下有可能最低价的有几个一样的，大家一起高亮
		var i=0;
		while(true){
			if(i==array.length){break;}
			show(array[i].carrierKey,"￥ "+array[i].lowerPrice,detailPlaneInfoTemplate(array[i]),"orange");
			if(i==array.length-1 || array[i+1].lowerPrice>array[i].lowerPrice){
				break;
			}
			i++;
		}
		
		show("ALL","￥ "+array[0].lowerPrice,"","black");
	}else{//没有任何结果
		show("ALL","无法比较","","orange");
	}
}


function detailPlaneInfoTemplate(data){
	var title="";
	title+="航班号："+data.flightId;
	title+="\n机型："+(data.type?data.type:"");
	title+="\n到达城市："+data.fromCityName;
	title+="\n起飞城市："+data.toCityName;
	var startTime=data.flightBatchStartTime;
	var endTime=data.flightBatchEndTime;
	title+="\n起飞时间："+startTime;
	title+="\n到达时间："+endTime;
	return title;
}




//td显示内容
function show(id,msg, info,color){
	var img=$("td[id='"+id+"']").children("img");
	$("td[id='"+id+"']").empty();
	$("td[id='"+id+"']").append(img);
	$(img).css({"display":"none"});
	$("td[id='"+id+"']").append(msg||"");
	$("td[id='"+id+"']").attr("title", info);
	if(!color){color="black";}
	$("td[id='"+id+"']").css({color:color});	
}
//td显示滚动的菊花~~~，会清空其他文字
function showWait(id){
	var img=$("td[id='"+id+"']").children("img");
	$("td[id='"+id+"']").empty();
	$("td[id='"+id+"']").append(img);
	$(img).css({"display":""});
}

function validateCity(){
	var form=$.trim($("#depCity").val());
	var to=$.trim($("#arrCity").val());
	if(form==""||form=="中文/拼音"){
		alert("【出发城市】不能为空");
		return false;
	}
	if(to==""||to=="中文/拼音"){
		alert("【到达城市】不能为空");
		return false;
	}
	if(form==to){
		alert("【出发城市】 和 【到达城市】 不能相同");
		return false;
	}
	return true;
}

function validateTime(){
	if($.trim($("#flightDate").val())=="" || $.trim($("#flightDate").val())==""){
		alert("请选择航班时间");
		return false;
	}
	return true;
}


</script>

</head>
<body>
<jsp:include page="../inc/treelist3.jsp"></jsp:include>
<jsp:include page="/pages/system/feedback/feedback.jsp" ></jsp:include>
<div class="main main_max" >
	<div class="sideBar">
	<div class="box">
		<div class="location">当前位置： 运价查询 &gt; 实时获取</div>
	</div>
	</div>
	<div class="form_body">
		<div class="main_max">
			<div class="form_table_tltle">查询条件</div>
	<form action="<%=request.getContextPath()%>/crawl/realtime/crawlOneDay.action" method="post" id="form1">
	<table cellSpacing=0 cellPadding=0 width="100%" border=0 class="form_table">
		
		<tr>
			<td class="left">航线：</td>
			<td class="">
			 <jsp:include page="../inc/flightSelect.jsp"></jsp:include>
			<!-- <input type="text" readonly="readonly" id="depCity" name="depCity"/>&nbsp;至&nbsp;
			<input type="text" readonly="readonly" id="arrCity" name="arrCity"/></td> -->
			<td></td>
		</tr>
		<tr>
			<td class="left">航班时间：</td>
			<td ><input type="text" readonly="readonly"  id="flightDate" name="flightDate"
			onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd',minDate:'%y-%M-%d'});"/>
				<input type="checkbox" name="flightTimes" value="AM" checked/>上午 
			  	<input type="checkbox" name="flightTimes" value="MD" checked/>中午
			  	<input type="checkbox" name="flightTimes" value="PM" checked/>下午
			  	<input type="checkbox" name="flightTimes" value="ND" checked/>晚上
			</td>
			<td>

			<!-- <input type="button" onclick="asyncCrawl();" value="" class="btn01" id="Submit1" />
			<select name="flag" id="flag">	
				<option value="1" selected>是</option>
				<option value="0">不是</option>
			</select>
			选择使用IP代理执行 -->
			</td>
			<td>
				<input type="button" onclick="notifyCrawl();" value="" class="btn01" id="Submit1" />
				&nbsp;
				<input type="reset" value="" class="btn02" id="Submit2"/>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type="checkbox" name="flag" id="y" value="1" checked/>使用代理IP
			</td>
		</tr>
	</table>
	</form>
	<div class="high_10"></div>
	<div class="form_table_tltle"><div style="float:left;">航线最低价</div></div>
				<table width="0" border="0" cellspacing="0" cellpadding="0" class="form_list">
					<tr class="title">
						<td style="width:100px;text-align: center">&nbsp;</td>
						<td style="text-align: center">所有</td>
						<td style="text-align: center">国航</td>
						<td style="text-align: center">南航</td>
						<td style="text-align: center">海航</td>
						<td style="text-align: center">深航</td>
						<td style="text-align: center">东航</td>
						<td style="text-align: center">厦航</td>
						<td style="text-align: center">山航</td>
						<td style="text-align: center">川航</td>
					</tr>
					<tr>
						<td class="line" style="text-align: center">直达</td>
						<td class="line_price" id="ALL" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="CA" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="CZ" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="HU" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="ZH" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="MU" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="MF" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="SC" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
						<td class="line_price" id="3U" style="text-align: center">
							<img src="<%=request.getContextPath()%>/images/blue-loading.gif" alt="查询中" style="display:none" class="juhua"/>
						</td>
					</tr>	
			</table>
</div>
</div>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<jsp:include page="../inc/footer2.jsp"></jsp:include> 
</body>
</html>